/*
 *
 * Copyright(c) TODO
 * licensing@realtor.org
 * http://www.realtor.org/license
 */

var results_grid;
Ext.onReady(function()
{
	AD.UI.get_sels_from_grid = function(cid)
	{
		var grid = Ext.getCmp(cid);
		return grid.getSelectionModel().getSelections();
	};

	AD.UI.grid_clear = function()
	{
		var grid = Ext.getCmp("results_grid");
		return grid.getSelectionModel().clearSelections();
	};

	var n_max_msg_count = 0;
	var selecting_all = false;

	AD.UI.grid_select_all =  function()
	{
		selecting_all = true;
		var grid = Ext.getCmp("results_grid");
		grid.getSelectionModel().selectAll();

		AD.UI.comps_panel.load(
		{
			 url : "templates/html/comps_selected.htm"
			,method : 'GET'
			,callback : AD.UI.comps_panel.fn_load_comps_detail_all
		});
		selecting_all = false;
	};

	var n_max_msg_count = 0;

	var handle_comp_sel = function(row)
	{
		AD.UI.map_goog_clear();
		AD.UI.map_bing_clear();

		if(true == selecting_all)
		{
			return;
		}

		var sel = AD.UI.get_sels_from_grid("results_grid");
		if (sel.length > AD.Globals.MAX_PROPS)
		{
			if (++n_max_msg_count < 2)
			{
				Ext.MessageBox.alert('Error', 'Please limit selection to ' + (AD.Globals.MAX_PROPS - 1) + ' comparables.');
			}
			AD.UI.results_grid.getSelectionModel().deselectRow(row);
			return false;
		}
		else
		{
			AD.UI.comps_panel.load(
			{
				url : "templates/html/comps_selected.htm"
				,method : 'GET'
				,callback : AD.UI.comps_panel.fn_load_comps_detail
			});
			n_max_msg_count = 0;
		}
	};

	var hinderSelection = false;

	var sm = new Ext.grid.CheckboxSelectionModel(
	{
		singleSelect : false,
		header : '',
		listeners :
		{
			handleMouseDown : Ext.emptyFn,
			rowdeselect : function(row) {handle_comp_sel(row);},
			rowselect : function(row) {handle_comp_sel(row);},
			beforerowselect : function(o, rowIndex, keepExisting, record)
			{
				if (hinderSelection)
				{
					hinderSelection = false;
					return false;
				}
			}
		}
	});

	AD.UI.results_grid = new Ext.grid.GridPanel(
	{
		id : "results_grid",
		title : 'Search Results',
		store : AD.Data.ds_search,

		autoScroll : true, collapsible : true, columnLines : true, frame : true,
		stateful :true,
		viewConfig:{forceFit : true},
		width : AD.Globals.form_width,
		height : AD.Globals.form_height,
		cm : new Ext.grid.ColumnModel(
		{
			defaults : {sortable : true, width :180},
			columns : [ sm,
			            {header : "#",					dataIndex : 'street_number',		align : 'right', width:100}
			 			,{header : "Street",			dataIndex : 'street_name'}
			 			,{header : "Unit",				dataIndex : 'unit_number',			align : 'right', width:100}
			            ,{header : "City",				dataIndex : 'city'}
			            ,{header : "State",				dataIndex : 'state'}
						,{header : "Zip",				dataIndex : 'zip',					align : 'right', width:100}

						,{header : "Status",			dataIndex : 'status'}
						,{header : "Listing Price",		dataIndex : 'listing_price',		align : 'right', renderer : AD.Format.format_money_00}
			            ,{header : "Selling Price",		dataIndex : 'selling_price',		align : 'right', renderer : AD.Format.format_money_00}
			            ,{header : "MLS Number",		dataIndex : 'ml_number',			align : 'right', id : 'mlnumber'}

			            //,{header : "Apn",				dataIndex : 'apn',					align : 'right'}
			            //,{header : "Financing",		dataIndex : 'financing'}
						,{header : "Listing Date",		dataIndex : 'listing_date',			align : 'right'}
						,{header : "Selling Date",		dataIndex : 'selling_date',			align : 'right'}
						,{header : "School District",	dataIndex : 'school_district'}

						,{header : "Acres",				dataIndex : 'acres',				align : 'right'}
						//,{header : "View",			dataIndex : 'view'}
						,{header : "Design / Style",	dataIndex : 'style'}
						,{header : "Exterior",			dataIndex : 'exterior'}

						,{header : "Age",				dataIndex : 'year_built',			align : 'right', renderer : AD.Format.format_num}
						,{header : "Bedrooms",			dataIndex : 'bed_rooms',			align : 'right', renderer : AD.Format.format_num}
				        ,{header : "Bathrooms",			dataIndex : 'bath_rooms',			align : 'right', renderer : AD.Format.format_num}

						,{header : "Sqft",				dataIndex : 'sqft_above_ground',	align : 'right', renderer : AD.Format.format_num}
						,{header : "Basement",			dataIndex : 'basement'}
						//,{header : "Basement Feat",	dataIndex : 'basement_feat'}
						//,{header : "Heating",			dataIndex : 'heating'}
						//,{header : "Cooling",			dataIndex : 'cooling'}
						,{header : "Garage",			dataIndex : 'garage',				align : 'right'}
						//,{header : "Fireplaces",		dataIndex : 'fireplaces',			align : 'right'}
			]
		}),
		sm : sm,
		renderTo : 'div_grid'
	});
});
